<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  input.time {
    border: none;
    font-size: 16px;
    padding-left: 10px;
  }
</style>
<template>
  <div class="oitem">
    <div class="weui-cells__title">货品明细 {{itemNo + 1}}
      <p id="acts" style="float: right;color: #1aad19;" @click="addItem" v-if="isLast">
        <span id="addItem" style="padding-right: 5px;">添加</span>
        <span v-if="itemNo != 0">|</span>
        <span id="delItem" style="padding-left: 5px;" @click="delItem" v-if="itemNo != 0">删除</span>
      </p>
    </div>
    <div class="weui-cell weui-cell_select fir">
      <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
      <div class="weui-cell__bd">
        <select class="weui-select product" v-model="productid" :name="'items[' + itemNo + '].productid'">
          <option value="" >请选择</option>
          <option selected=""  v-for="(item, index) in products" :key="item.productid" :value="item.productid">{{item.productname}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select" v-if="picktype == 2">
      <div class="weui-cell__hd"><label class="weui-label">规格 </label></div>
      <div class="weui-cell__bd">
        <select class="weui-select" :name="'items[' + itemNo + '].specguid'" v-model="speguid">
          <option selected="" v-if="productid == item.productid"  v-for="(item, index) in specs" :key="item.spguid" :value="item.spguid">{{item.specName}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell weui-cell_select">
      <div class="weui-cell__hd"><label class="weui-label">库间 </label></div>
      <div class="weui-cell__bd">
        <select class="weui-select" :name="'items[' + itemNo + '].shguid'">
          <option selected=""  v-for="(item, index) in houses" :key="item.guid" :value="item.guid">{{item.name}}</option>
        </select>
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__hd"><label class="weui-label">重量(吨)</label></div>
      <div class="weui-cell__bd">
        <input class="weui-input weight" type="number" :name="'items[' + itemNo + '].weight'" placeholder="请输入重量">
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChangeSaItem',
  data () {
    return {
      products: [],
      productid: '',
      specs: [],
      speguid: '',
      houses: [],
      openid: localStorage.getItem('openid')
    }
  },
  props: {
    itemNo: {
      type: Number
    },
    isLast: {
      type: Boolean
    },
    orgid: {
      default: ''
    },
    strmid: {
      default: ''
    },
    type: {
      type: Number
    },
    picktype: {
      type: Number
    },
  },
  computed: {
  },
  mounted() {
    this.getProducts();
  },
  watch: {
    'strmid': function () {
      this.getProducts();
      if (this.type == 1) {
        this.getPLInfo();
        this.getSpesByWinfo();
      } else {
        this.getSh();
      }
    },
    'picktype': function () {
      this.getProducts();
    },
    'orgid': function (val) {
      this.getProducts();
      if (this.type == 1) {
        this.getPLInfo();
        this.getSpesByWinfo();
      }
    },
    'type': function (val) {
      if (this.type == 1) {
        this.getPLInfo();
        this.getSpesByWinfo();
      } else {
        this.getSh();
        this.getSpec();
      }
    },
    'productid': function (val) {
      if (this.type == 1) {
        this.getPLInfo();
        this.getSpesByWinfo();
      }
    },
    'speguid': function (val) {
      if (this.type == 1) {
        this.getPLInfo();
      }
    },
    'picktype': function (val) {
      this.getProducts();
      if (this.type == 1) {
        this.getPLInfo();
        this.getSpesByWinfo();
      } else {
        this.getSh();
      }
    },
  },
  methods: {
    addItem: function() {
      this.$emit('doAddItem');
    },
    delItem: function() {
      this.$emit('doDelItem');
    },
    getProducts: function() {
      if (this.orgid == '' || this.strmid == '' || this.picktype == '') return;
      this.axios({
        method: 'get',
        url: '/sales/Whoutform!getPdByWinfo.action?openid=' + this.openid,
        params: {orgid: this.orgid, strmid: this.strmid, picktype: this.picktype}
      }).then((response) => {
        if (response.data.products != undefined) {
          this.products = response.data.products;
          this.productid = response.data.products[0].productid;
        } else {
          this.toastError("系统错误");
        }

      })
    },
    getPLInfo: function() {
      if (this.orgguid == '' || this.strmid == '' || this.productid == '') {
        return;
      }
      this.axios({
        method: 'get',
        url: '/sales/Whoutform!getPLInfo.action?openid=' + this.openid,
        params: {orgid: this.orgguid, strmid: this.strmid, productid: this.productid, spec: this.speguid, picktype: this.picktype}
      }).then((response) => {
        if (response.data.houses != undefined) {
          this.storehouses = response.data.houses;
          this.shguid = response.data.houses[0].guid;
        } else {
          this.toastError("系统错误");
        }

      })
    },
    getSh: function() {
      if (this.strmid == '') {
        return;
      }
      this.axios({
        method: 'get',
        url: '/sales/Whoutform!getSh.action?openid=' + this.openid,
        params: {srguid: this.strmid, shrtype: this.picktype}
      }).then((response) => {
        if (response.data.list != undefined) {
          this.storehouses = response.data.list;
          this.shguid = response.data.list[0].guid;
        } else {
          this.toastError("系统错误");
        }

      })
    },
    getSpec: function() {
      this.axios({
        method: 'get',
        url: '/sales/Sellform!findSalesspecification.action?openid=' + this.openid,
        params: {}
      }).then((response) => {
        if (response.data.status == 'true') {
          this.specs = response.data.result;
          if (response.data.result.length > 0) {
            this.speguid = response.data.result[0].guid;
          }
        } else {
          this.toastError(response.data.result);
        }

      })
    },
    getSpesByWinfo: function() {
      if (this.orgguid == '' || this.strmid == '' || this.productid == '') {
        return;
      }
      this.axios({
        method: 'get',
        url: '/sales/Whoutform!getSpesByWinfo.action?openid=' + this.openid,
        params: {orgid: this.orgid, strmid: this.strmid, productid: this.productid, picktype: this.picktype}
      }).then((response) => {
        if (response.data.spes != undefined) {
          this.specs = response.data.spes;
          this.speguid = response.data.spes[0].guid;
        } else {
          this.toastError('系统错误');
        }

      })
    },
  }
}
</script>
